package ru.minidoc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import ru.minidoc.dao.DocumentDao;
import ru.minidoc.model.Document;
import ru.minidoc.model.User;
import ru.minidoc.model.json.DocumentData;
import ru.minidoc.model.json.DocumentList;

@Controller
public class DocumentController {
    @Autowired
    private DocumentDao documentDao;

    @RequestMapping(value = "/document/read.action", method = RequestMethod.GET)
    public @ResponseBody
    DocumentList read() {
        User user = (User) RequestContextHolder.currentRequestAttributes().getAttribute("user",
                RequestAttributes.SCOPE_SESSION);
        List<Document> documentList = documentDao.getDocumentList(user);
        return new DocumentList(documentList.size(), documentList);
    }

    @RequestMapping(value = "/document/create.action", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Integer> create(@RequestBody DocumentData documentList) {
        User user = (User) RequestContextHolder.currentRequestAttributes().getAttribute("user",
                RequestAttributes.SCOPE_SESSION);
        Map<String, Integer> result = new HashMap<String, Integer>();
        Integer id = documentDao.create(documentList.rows, user);
        result.put("id", id);
        return result;
    }

    @RequestMapping(value = "/document/update.action", method = RequestMethod.POST)
    public @ResponseBody
    void update(@RequestBody DocumentData documentList) {
        documentDao.update(documentList.rows);
    }

    @RequestMapping(value = "/document/destroy.action", method = RequestMethod.POST)
    public @ResponseBody
    String destroy(@RequestBody Map<String, Integer> rows) {
        documentDao.delete(new Document(rows.get("rows")));
        return "1";
    }

    @RequestMapping(value = "/document/save.action", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> save(@RequestBody Map<String, String> json) {
        User user = (User) RequestContextHolder.currentRequestAttributes().getAttribute("user",
                RequestAttributes.SCOPE_SESSION);
        Document document = documentDao.load(new Document(Integer.valueOf(json.get("id"))));
        document.setText(json.get("text"));
        documentDao.saveText(document, user);
        return new HashMap<String, String>();
    }

    @RequestMapping(value = "/document/load.action", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, String> load(@RequestBody Map<String, Integer> json) {
        Map<String, String> result = new HashMap<String, String>();
        Document document = documentDao.load(new Document(json.get("id")));
        result.put("id", json.get("id").toString());
        result.put("text", document.getText());
        return result;
    }
}
